#include <sstream>
#include "AdminDatabase.h"
#include "AdminDbValue.h"
#include "AdminTools.h"

AgoraAdmin::Database::Database()
{
}

const unsigned int	AgoraAdmin::Database::Open(const std::string& dbPath)
{
	if (sqlite3_open(dbPath.c_str(), &_db) != SQLITE_OK)
		return -1;
	return 0;
}

const unsigned int	AgoraAdmin::Database::Close()
{
	sqlite3_close(_db);
	return 0;
}

const unsigned int	AgoraAdmin::Database::Execute(const std::string& statement, DbResult* result)
{
	sqlite3_stmt*	stmt;
	int				step_rc;
	int				step_nbCol;

	if (sqlite3_prepare(_db, statement.c_str(), statement.length(), &stmt, 0) != SQLITE_OK)
		return -1;
	if ((step_nbCol = sqlite3_column_count(stmt)) > 0)
	{
		DbValue*	ptrnull = 0;
		while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW)
		{
			std::map<std::string, IDbValue*>*	row = new std::map<std::string, IDbValue*>();
			int	iCol = 0;
			while (iCol < step_nbCol)
			{
				if (sqlite3_column_type(stmt, iCol) == SQLITE_NULL)
					row->insert(std::make_pair(std::string(sqlite3_column_name(stmt, iCol)), ptrnull));
				if (sqlite3_column_type(stmt, iCol) == SQLITE_INTEGER)
					row->insert(std::make_pair(std::string(sqlite3_column_name(stmt, iCol)), new DbValue(sqlite3_column_int(stmt, iCol))));
				if (sqlite3_column_type(stmt, iCol) == SQLITE_FLOAT)
					row->insert(std::make_pair(std::string(sqlite3_column_name(stmt, iCol)), new DbValue(sqlite3_column_double(stmt, iCol))));
				if (sqlite3_column_type(stmt, iCol) == SQLITE_TEXT)
				{
					std::stringstream	text;
					text << sqlite3_column_text(stmt, iCol);
					row->insert(std::make_pair(std::string(sqlite3_column_name(stmt, iCol)), new DbValue(text.str())));
				}
				iCol++;
			}
			result->push_back(row);
		}
		if (step_rc != SQLITE_DONE)
		{
			//check value et diff return, todo gestion erreur
			return -1;
		}
	}
	if (sqlite3_finalize(stmt) != SQLITE_OK)
		return -1;
	return 0;
}

const unsigned int	AgoraAdmin::Database::Execute(const std::string& statement)
{
	sqlite3_stmt*	stmt;
	int				step_rc;

	if (sqlite3_prepare(_db, statement.c_str(), statement.length(), &stmt, 0) != SQLITE_OK)
		return -1;
	if ((step_rc = sqlite3_step(stmt)) != SQLITE_DONE)
	{
		//check value et diff return, todo gestion erreur
		return -1;
	}
	if (sqlite3_finalize(stmt) != SQLITE_OK)
		return -1;
	return 0;
}

AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectAllUser()
{
	//load depuis la base tout les users
	DbResult*	result = new DbResult();
	this->Execute("SELECT * FROM users;", result);
	return result;
}

AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectUser(const unsigned int id)
{
	//load depuis la base le user 'id'
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM users WHERE id=" << id << ";";
	this->Execute(query.str(), result);
	return result;
}

AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectUser(const std::string& login)
{
	//load depuis la base le user 'login'
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM users WHERE login='" << AgoraAdmin::Tools::UrlEncodeString(login) << "';";
	this->Execute(query.str(), result);
	return result;
}

void				AgoraAdmin::Database::UpdateUser(const unsigned int id, const std::string& login, const std::string& password)
{
	std::stringstream	query;
	query << "UPDATE users SET login='" << AgoraAdmin::Tools::UrlEncodeString(login) << "', password='" << AgoraAdmin::Tools::UrlEncodeString(password) << "' WHERE id=" << id << ";";
	this->Execute(query.str());
}

const unsigned int	AgoraAdmin::Database::CreateUser(const std::string& login, const std::string& password)
{
	std::stringstream	query;
	query << "INSERT INTO users(login, password) VALUES('" << AgoraAdmin::Tools::UrlEncodeString(login) << "','" << AgoraAdmin::Tools::UrlEncodeString(password) << "');";
	this->Execute(query.str());
	/*DbResult*	result = this->SelectUser(login);
	unsigned int	id = (*result->at(0))["id"]->GetValueInt();
	delete result;*/
	return this->SelectUserId(login);
}

void				AgoraAdmin::Database::DeleteAllUser()
{
	std::stringstream	query;
	query << "DELETE FROM users;";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::DeleteUser(const unsigned int id)
{
	std::stringstream	query;
	query << "DELETE FROM users WHERE id=" << id << ";";
	this->Execute(query.str());
}

//// User db funtions
const unsigned int	AgoraAdmin::Database::SelectUserId(const std::string& login)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT id FROM users WHERE login='" << AgoraAdmin::Tools::UrlEncodeString(login) << "';";
	this->Execute(query.str(), &result);
	return (*result[0])["id"]->GetValueInt();
}

std::string			AgoraAdmin::Database::SelectUserLogin(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT login FROM users WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["login"]->GetValueString();
}

void				AgoraAdmin::Database::UpdateUserLogin(const unsigned int id, const std::string& login)
{
	std::stringstream	query;
	query << "UPDATE users SET login='" << AgoraAdmin::Tools::UrlEncodeString(login) << "' WHERE id=" << id << ";";
	this->Execute(query.str());
}

std::string			AgoraAdmin::Database::SelectUserPassword(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT password FROM users WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["password"]->GetValueString();
}

void				AgoraAdmin::Database::UpdateUserPassword(const unsigned int id, const std::string& password)
{
	std::stringstream	query;
	query << "UPDATE main.users SET password='" << AgoraAdmin::Tools::UrlEncodeString(password) << "' WHERE id=" << id << ";";
	this->Execute(query.str());
}

//// ChannelManager db functions
AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectAllChannel()
{
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM channels;";
	this->Execute(query.str(), result);
	return result;
}

AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectChannel(const unsigned int idParent, const std::string& name)
{
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM channels WHERE idParent=" << idParent << " AND name='" << AgoraAdmin::Tools::UrlEncodeString(name) << "';";
	this->Execute(query.str(), result);
	return result;
}

const unsigned int	AgoraAdmin::Database::CreateChannel(const unsigned int idParent, const std::string& name, const unsigned int maxNameLenght, const std::string& password, const unsigned int maxPasswordLenght, const std::string& motd, const unsigned int maxMotdLenght, const std::string& description, const unsigned int maxDescriptionLenght, const bool visible, const unsigned int maxUser, const unsigned int maxTalker)
{
	std::stringstream	query;
	query << "INSERT INTO channels(idParent, name, maxNameLenght, password, maxPasswordLenght, motd, maxMotdLenght, description, maxDescriptionLenght, visibility, maxUser, maxTalker) VALUES(" << idParent << ",'" << AgoraAdmin::Tools::UrlEncodeString(name) << "'," << maxNameLenght << ",'" << AgoraAdmin::Tools::UrlEncodeString(password) << "'," << maxPasswordLenght << ",'" << AgoraAdmin::Tools::UrlEncodeString(motd) << "'," << maxMotdLenght << ",'" << AgoraAdmin::Tools::UrlEncodeString(description) << "'," << maxDescriptionLenght << "," << visible << "," << maxUser << "," << maxTalker << ");";
	this->Execute(query.str());
	return this->SelectChannelId(idParent, name);
}

void				AgoraAdmin::Database::DeleteAllChannel()
{
	std::stringstream	query;
	query << "DELETE FROM channels";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::DeleteChannel(const unsigned int id)
{
	std::stringstream	query;
	query << "DELETE FROM channels WHERE id=" << id << ";";
	this->Execute(query.str());
}

//// Channel db functions
const unsigned int	AgoraAdmin::Database::SelectChannelId(const unsigned int idParent, const std::string& name)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT id FROM channels WHERE idParent=" << idParent << " AND name='" << AgoraAdmin::Tools::UrlEncodeString(name) << "';";
	this->Execute(query.str(), &result);
	return (*result[0])["id"]->GetValueInt();
}

const unsigned int	AgoraAdmin::Database::SelectChannelIdParent(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT idParent FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["idParent"]->GetValueInt();
}

void				AgoraAdmin::Database::UpdateChannelIdParent(const unsigned int id, const unsigned int idParent)
{
	std::stringstream	query;
	query << "UPDATE channels SET idParent=" << idParent << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

const std::string&	AgoraAdmin::Database::SelectChannelName(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT name FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["name"]->GetValueString();
}

void				AgoraAdmin::Database::UpdateChannelName(const unsigned int id, const std::string& name)
{
	std::stringstream	query;
	query << "UPDATE channels SET name='" << AgoraAdmin::Tools::UrlEncodeString(name) << "' WHERE id=" << id << ";";
	this->Execute(query.str());
}

const unsigned int	AgoraAdmin::Database::SelectChannelMaxNameLenght(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT maxNameLenght FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["maxNameLenght"]->GetValueInt();
}

void				AgoraAdmin::Database::UpdateChannelMaxNameLenght(const unsigned int id, const unsigned int maxNameLenght)
{
	std::stringstream	query;
	query << "UPDATE channels SET maxNameLenght=" << maxNameLenght << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

const std::string&	AgoraAdmin::Database::SelectChannelPassword(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT password FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["password"]->GetValueString();
}

void				AgoraAdmin::Database::UpdateChannelPassword(const unsigned int id, const std::string& password)
{
	std::stringstream	query;
	query << "UPDATE channels SET password='" << AgoraAdmin::Tools::UrlEncodeString(password) << "' WHERE id=" << id << ";";
	this->Execute(query.str());
}

const unsigned int	AgoraAdmin::Database::SelectChannelMaxPasswordLenght(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT maxPasswordLenght FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["maxPasswordLenght"]->GetValueInt();
}

void				AgoraAdmin::Database::UpdateChannelMaxPasswordLenght(const unsigned int id, const unsigned int maxPasswordLenght)
{
	std::stringstream	query;
	query << "UPDATE channels SET maxPasswordLenght=" << maxPasswordLenght << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

const std::string&	AgoraAdmin::Database::SelectChannelMotd(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT motd FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["motd"]->GetValueString();
}

void				AgoraAdmin::Database::UpdateChannelMotd(const unsigned int id, const std::string& motd)
{
	std::stringstream	query;
	query << "UPDATE channels SET motd='" << AgoraAdmin::Tools::UrlEncodeString(motd) << "' WHERE id=" << id << ";";
	this->Execute(query.str());
}

const unsigned int	AgoraAdmin::Database::SelectChannelMaxMotdLenght(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT maxMotdLenght FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["maxMotdLenght"]->GetValueInt();
}

void				AgoraAdmin::Database::UpdateChannelMaxMotdLenght(const unsigned int id, const unsigned int maxMotdLenght)
{
	std::stringstream	query;
	query << "UPDATE channels SET maxMotdLenght=" << maxMotdLenght << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

const std::string&	AgoraAdmin::Database::SelectChannelDescription(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT description FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["description"]->GetValueString();
}

void				AgoraAdmin::Database::UpdateChannelDescription(const unsigned int id, const std::string& description)
{
	std::stringstream	query;
	query << "UPDATE channels SET description='" << AgoraAdmin::Tools::UrlEncodeString(description) << "' WHERE id=" << id << ";";
	this->Execute(query.str());
}

const unsigned int	AgoraAdmin::Database::SelectChannelMaxDesciptionLenght(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT maxDescriptionLenght FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["maxDescriptionLenght"]->GetValueInt();
}

void				AgoraAdmin::Database::UpdateChannelMaxDesciptionLenght(const unsigned int id, const unsigned int maxDescriptionLenght)
{
	std::stringstream	query;
	query << "UPDATE channels SET maxDescriptionLenght=" << maxDescriptionLenght << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

const bool			AgoraAdmin::Database::SelectChannelVisibility(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT visible FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["visible"]->GetValueBool();
}

void				AgoraAdmin::Database::UpdateChannelVisibility(const unsigned int id, const bool visible)
{
	std::stringstream	query;
	query << "UPDATE channels SET visibility=" << visible << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

const unsigned int	AgoraAdmin::Database::SelectChannelMaxUser(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT maxUser FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["maxUser"]->GetValueInt();
}

void				AgoraAdmin::Database::UpdateChannelMaxUser(const unsigned int id, const unsigned int maxUser)
{
	std::stringstream	query;
	query << "UPDATE channels SET maxUser=" << maxUser << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

const unsigned int	AgoraAdmin::Database::SelectChannelMaxTalker(const unsigned int id)
{
	DbResult	result;
	std::stringstream	query;
	query << "SELECT maxTalker FROM channels WHERE id=" << id << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["maxTalker"]->GetValueInt();
}

void				AgoraAdmin::Database::UpdateChannelMaxTalker(const unsigned int id, const unsigned int maxTalker)
{
	std::stringstream	query;
	query << "UPDATE channels SET maxTalker=" << maxTalker << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectChannelBanListLogin(unsigned int id)
{
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT login FROM banListLoginChannel WHERE idChannel=" << id << ";";
	this->Execute(query.str(), result);
	return result;
}

void				AgoraAdmin::Database::InsertChannelBanListLogin(const unsigned int id, const unsigned int idUser)
{
	std::stringstream	query;
	query << "INSERT INTO banListLoginChannel(idChannel, idUser) VALUES(" << id << "," << idUser << ");";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::DeleteChannelBanListLogin(const unsigned int id, const unsigned int idUser)
{
	std::stringstream	query;
	query << "DELETE FROM banListLoginChannel WHERE idChannel=" << id << " AND idUser=" << idUser << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::DeleteChannelBanList(const unsigned int id)
{
	std::stringstream	query;
	query << "DELETE FROM banListLoginChannel WHERE idChannel=" << id << ";";
	this->Execute(query.str());
}

AgoraAdmin::IDatabase::DbResult*		AgoraAdmin::Database::SelectChannelRight(const unsigned int id)
{
	//load depuis la base le right de id
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM channelRights WHERE id=" << id << ";";
	this->Execute(query.str(), result);
	return result;
}

AgoraAdmin::IDatabase::DbResult*		AgoraAdmin::Database::SelectChannelRight(const unsigned int idUser, const unsigned int idChannel)
{
	//load depuis la base le right de user/channel
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM channelRights WHERE idUser=" << idUser << " AND idChannel=" << idChannel << ";";
	this->Execute(query.str(), result);
	return result;
}

const unsigned int		AgoraAdmin::Database::SelectChannelRightId(const unsigned int idUser, const unsigned int idChannel)
{
	//load depuis la base le id d'un right
	DbResult	result;
	std::stringstream	query;
	query << "SELECT id FROM channelRights WHERE idUser=" << idUser << " AND idChannel=" << idChannel << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["id"]->GetValueInt();
}

const unsigned int	AgoraAdmin::Database::CreateChannelRightDefault(const unsigned int idUser, const unsigned int idChannel)
{
	DbResult*	result = this->SelectChannelRight(0);
	std::stringstream	query;
	query << "INSERT INTO channelRights(idUser, idChannel, canCreate, canDelete, canEdit, canEditUser, canTalk, canWrite, canKick, canBan) VALUES(" << idUser << "," << idChannel << "," << (*result->at(0))["canCreate"]->GetValueInt() << "," << (*result->at(0))["canDelete"]->GetValueInt() << "," << (*result->at(0))["canEdit"]->GetValueInt() << "," << (*result->at(0))["canEditUser"]->GetValueInt() << "," << (*result->at(0))["canTalk"]->GetValueInt() << "," << (*result->at(0))["canWrite"]->GetValueInt() << "," << (*result->at(0))["canKick"]->GetValueInt() << "," << (*result->at(0))["canBan"]->GetValueInt() << ");";
	this->Execute(query.str());
	delete result;
	return this->SelectChannelRightId(idUser, idChannel);
}

void				AgoraAdmin::Database::UpdateChannelRight(const unsigned int id, const bool canCreate, const bool canDelete, const bool canEdit, const bool canEditUser, const bool canTalk, const bool canWrite, const bool canKick, const bool canBan)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canCreate=" << (int)canCreate << ", canDelete=" << (int)canDelete << ", canEdit=" << (int)canEdit << ", canEditUser=" << (int)canEditUser << ", canTalk=" << (int)canTalk << ", canWrite=" << (int)canWrite << ", canKick=" << (int)canKick << ", canBan=" << (int)canBan << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateChannelRightCanCreateChild(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canCreate=" << (int)value << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateChannelRightCanDeleteChild(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canDelete=" << (int)value << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateChannelRightCanEdit(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canEdit=" << (int)value << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateChannelRightCanEditUserRight(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canEditUser=" << (int)value << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateChannelRightCanTalk(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canTalk=" << (int)value << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateChannelRightCanWrite(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canWrite=" << (int)value << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateChannelRightCanKick(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canKick=" << (int)value << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateChannelRightCanBan(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE channelRights SET canBan=" << (int)value << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

const std::string&	AgoraAdmin::Database::SelectHostName()
{
	//load depuis la base le name du host
	DbResult	result;
	std::stringstream	query;
	query << "SELECT name FROM host WHERE id=0;";
	this->Execute(query.str(), &result);
	return (*result[0])["name"]->GetValueString();
}

void				AgoraAdmin::Database::UpdateHostName(const std::string& name)
{
	std::stringstream	query;
	query << "UPDATE host SET name='" << AgoraAdmin::Tools::UrlEncodeString(name) << "' WHERE id=0;";
	this->Execute(query.str());
}

AgoraAdmin::IDatabase::DbResult*		AgoraAdmin::Database::SelectHostBanListUser()
{
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT idUser FROM banListLogin;";
	this->Execute(query.str(), result);
	return result;
}

void				AgoraAdmin::Database::InsertHostBanListUser(const unsigned int idUser)
{
	std::stringstream	query;
	query << "INSERT INTO banListLogin(idUser) VALUES(" << idUser << ");";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::DeleteHostBanListUser(const unsigned int idUser)
{
	std::stringstream	query;
	query << "DELETE FROM banListLogin WHERE idUser=" << idUser << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::DeleteChannelRight(const unsigned int idUser, const unsigned int idChannel)
{
	std::stringstream	query;
	query << "DELETE FROM channelRights WHERE idUser=" << idUser << " AND idChannel=" << idChannel << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::DeleteChannelRightChannel(const unsigned int idChannel)
{
	std::stringstream	query;
	query << "DELETE FROM channelRights WHERE idChannel=" << idChannel << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::DeleteChannelRightUser(const unsigned int idUser)
{
	std::stringstream	query;
	query << "DELETE FROM channelRights WHERE idUser=" << idUser << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRight(const unsigned int id, const bool canChangeLogin, const bool canChangePassword, const bool canChangeChannel, const bool canCreateChannel, const bool canDeleteChannel, const bool canMoveChannel, const bool canKickUser, const bool canBanUser, const bool canEditUser)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canChangeLogin=" << (int)canChangeLogin << ", canChangePassword=" << (int)canChangePassword << ", canChangeChannel=" << (int)canChangeChannel << ", canCreateChannel=" << (int)canCreateChannel << ", canDeleteChannel=" << (int)canDeleteChannel << ", canMoveChannel=" << (int)canMoveChannel << ", canKickUser=" << (int)canKickUser << ", canBanUser=" << (int)canBanUser << ", canEditUser=" << (int)canEditUser << " WHERE id=" << id << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanChangeLogin(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canChangeLogin=" << (int)value << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanChangePassword(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canChangePassword=" << (int)value << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanChangeChannel(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canCanChangeChannel=" << (int)value << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanCreateChannel(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canCreateChannel=" << (int)value << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanDeleteChannel(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canDeleteChannel=" << (int)value << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanMoveChannel(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canMoveChannel=" << (int)value << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanKickUser(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canKickUser=" << (int)value << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanBanUser(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canBanUser=" << (int)value << ";";
	this->Execute(query.str());
}

void				AgoraAdmin::Database::UpdateHostRightCanEditUser(const unsigned int id, const bool value)
{
	std::stringstream	query;
	query << "UPDATE hostRights SET canEditUser=" << (int)value << ";";
	this->Execute(query.str());
}

AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectHostRight(const unsigned int idUser)
{
	//load depuis la base le right de id
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM hostRights WHERE idUser=" << idUser << ";";
	this->Execute(query.str(), result);
	return result;
}

AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectHostRightDefault()
{
	//load depuis la base le right de id
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM hostRights WHERE id=0;";
	this->Execute(query.str(), result);
	return result;
}

AgoraAdmin::IDatabase::DbResult*			AgoraAdmin::Database::SelectHostRightUser(const unsigned int idUser)
{
	//load depuis la base le right de user
	DbResult*	result = new DbResult();
	std::stringstream	query;
	query << "SELECT * FROM hostRights WHERE idUser=" << idUser << ";";
	this->Execute(query.str(), result);
	return result;
}

const unsigned int	AgoraAdmin::Database::SelectHostRightId(const unsigned int idUser)
{
	//load depuis la base le id d'un right
	DbResult	result;
	std::stringstream	query;
	query << "SELECT id FROM hostRights WHERE idUser=" << idUser << ";";
	this->Execute(query.str(), &result);
	return (*result[0])["id"]->GetValueInt();
}

const unsigned int	AgoraAdmin::Database::CreateHostRightDefault(const unsigned int idUser)
{
	DbResult*	result = this->SelectHostRightDefault();
	std::stringstream	query;
	query << "INSERT INTO hostRights(idUser, canChangeLogin, canChangePassword, canChangeChannel, canCreateChannel, canDeleteChannel, canMoveChannel, canKickUser, canBanUser, canEditUser) VALUES(" << idUser << "," << (*result->at(0))["canChangeLogin"]->GetValueInt() << "," << (*result->at(0))["canChangePassword"]->GetValueInt() << "," << (*result->at(0))["canChangeChannel"]->GetValueInt() << "," << (*result->at(0))["canCreateChannel"]->GetValueInt() << "," << (*result->at(0))["canDeleteChannel"]->GetValueInt() << "," << (*result->at(0))["canMoveChannel"]->GetValueInt() << "," << (*result->at(0))["canKickUser"]->GetValueInt() << "," << (*result->at(0))["canBanUser"]->GetValueInt() << "," << (*result->at(0))["canEditUser"]->GetValueInt() << ");";
	this->Execute(query.str());
	delete result;
	return this->SelectHostRightId(idUser);
}

void				AgoraAdmin::Database::DeleteHostRightUser(const unsigned int idUser)
{
	std::stringstream	query;
	query << "DELETE FROM hostRights WHERE idUser=" << idUser << ";";
	this->Execute(query.str());
}
